﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using JobDescOnline.Presentation.Models;
using eq.CoreBase.View;
using eq.CoreBase.Control;

namespace JobDescOnline.Presentation.Controllers
{
    public class JobDescResponsibilityController : Controller
    {
        public JobDescDBEntities1 db = new JobDescDBEntities1();

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    JobDescResponsibility jobRespon = new JobDescResponsibility
                    {
                        Rowstatus = true,
                        JobDescriptionID = Convert.ToInt32(collection["JobDescriptionID"]),
                        MainTask = collection["MainTask"],
                        Activities = collection["Activities"],
                        CreatedBy = UserAuthenticated.isAuthenticated ? UserAuthenticated.authUser.FirstName : "DEFAULT",
                        CreatedOn = DateTime.Now
                    };

                    db.JobDescResponsibilities.AddObject(jobRespon);
                    db.SaveChanges();
                }

                int id = Convert.ToInt32(collection["JobDescriptionID"]);

                IEnumerable<JobDescResponsibility> jobdescresponsibility = db.JobDescResponsibilities.Where(d => d.JobDescriptionID == id);
                List<JobDescResponsibilityModel> jobdescresponsibilitymodel = new List<JobDescResponsibilityModel>();
                JobDescResponsibilityModel model = new JobDescResponsibilityModel();
                foreach (JobDescResponsibility obj in jobdescresponsibility)
                {
                    var item = from p in db.PerformanceIndicators
                               select new PerformanceIndicatorModel
                               {
                                   ID = p.ID,
                                   Rowstatus = p.Rowstatus,
                                   JobDescResponsibilityID = p.JobDescResponsibilityID,
                                   PI = p.PI,
                                   CreatedBy = p.CreatedBy,
                                   CreatedOn = p.CreatedOn,
                                   LastModifiedBy = p.LastModifiedBy,
                                   LastModifiedOn = p.LastModifiedOn
                               };
                    item = item.Where(p => p.JobDescResponsibilityID == obj.ID);

                    model = new JobDescResponsibilityModel();
                    model.ID = obj.ID;
                    model.Rowstatus = obj.Rowstatus;
                    model.JobDescriptionID = obj.JobDescriptionID;
                    model.MainTask = obj.MainTask;
                    model.Activities = obj.Activities;
                    model.CreatedBy = obj.CreatedBy;
                    model.CreatedOn = obj.CreatedOn;
                    model.LastModifiedBy = obj.LastModifiedBy;
                    model.LastModifiedOn = obj.LastModifiedOn;
                    model.PI = item.ToList();

                    jobdescresponsibilitymodel.Add(model);
                }

                return View(jobdescresponsibilitymodel);
            }
            catch
            {
                return View();
            }
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    JobDescResponsibility jobRespon = db.JobDescResponsibilities.First(d => d.ID == id);
                    jobRespon.Rowstatus = true;
                    jobRespon.JobDescriptionID = Convert.ToInt32(collection["JobDescriptionID"]);
                    jobRespon.MainTask = collection["MainTask"];
                    jobRespon.Activities = collection["Activities"];
                    jobRespon.LastModifiedBy = UserAuthenticated.isAuthenticated ? UserAuthenticated.authUser.FirstName : "DEFAULT";
                    jobRespon.LastModifiedOn = DateTime.Now;

                    db.JobDescResponsibilities.ApplyCurrentValues(jobRespon);
                    db.SaveChanges();
                }

                int ID = Convert.ToInt32(collection["JobDescriptionID"]);
                IEnumerable<JobDescResponsibility> jobdescresponsibility = db.JobDescResponsibilities.Where(d => d.JobDescriptionID == ID);
                List<JobDescResponsibilityModel> jobdescresponsibilitymodel = new List<JobDescResponsibilityModel>();
                JobDescResponsibilityModel model = new JobDescResponsibilityModel();
                foreach (JobDescResponsibility obj in jobdescresponsibility)
                {
                    var item = from p in db.PerformanceIndicators
                               select new PerformanceIndicatorModel
                               {
                                   ID = p.ID,
                                   Rowstatus = p.Rowstatus,
                                   JobDescResponsibilityID = p.JobDescResponsibilityID,
                                   PI = p.PI,
                                   CreatedBy = p.CreatedBy,
                                   CreatedOn = p.CreatedOn,
                                   LastModifiedBy = p.LastModifiedBy,
                                   LastModifiedOn = p.LastModifiedOn
                               };
                    item = item.Where(p => p.JobDescResponsibilityID == obj.ID);

                    model = new JobDescResponsibilityModel();
                    model.ID = obj.ID;
                    model.Rowstatus = obj.Rowstatus;
                    model.JobDescriptionID = obj.JobDescriptionID;
                    model.MainTask = obj.MainTask;
                    model.Activities = obj.Activities;
                    model.CreatedBy = obj.CreatedBy;
                    model.CreatedOn = obj.CreatedOn;
                    model.LastModifiedBy = obj.LastModifiedBy;
                    model.LastModifiedOn = obj.LastModifiedOn;
                    model.PI = item.ToList();

                    jobdescresponsibilitymodel.Add(model);
                }

                return View(jobdescresponsibilitymodel);
            }
            catch
            {
                return View();
            }
        }
 
        public ActionResult Delete(int id)
        {
            JobDescResponsibility jobrespon = db.JobDescResponsibilities.Single(d => d.ID == id);
            int ID = Convert.ToInt32(jobrespon.JobDescriptionID);
            db.JobDescResponsibilities.DeleteObject(jobrespon);

            var model = db.JobDescResponsibilities.Where(d => d.JobDescriptionID == ID);
            db.SaveChanges();
            return View(model);
        }
    }
}
